<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind" label-width="120px">
      <uni-forms-item name="customerId" label="客户">
        <uni-data-select
        	 collection="mydb-customer"
        	 field="_id as value, name as text"
        	 v-model="formData.customerId"
			 disabled
         />
      </uni-forms-item>
	   <view class="uni-title">产品信息</view>
      <uni-forms-item name="productId" label="匹配产品" required>
        <uni-data-select
        	 collection="mydb-product-list"
        	 field="_id as value, title as text"
        	 v-model="formData.productId"
         />
      </uni-forms-item>
         <uni-forms-item name="productIdType" label="产品类型">
              <uni-data-checkbox v-model="formData.productIdType" :localdata="formOptions.productIdType_localdata"></uni-data-checkbox>
            </uni-forms-item>
            <uni-forms-item name="limit" label="审批金额">
              <uni-easyinput placeholder="审批金额" v-model="formData.limit"></uni-easyinput>
            </uni-forms-item>
            <uni-forms-item name="transfer" label="放款金额">
              <uni-easyinput placeholder="放款金额" v-model="formData.transfer"></uni-easyinput>
            </uni-forms-item>
            <uni-forms-item name="loanPeriod" label="贷款期限(月)">
              <uni-data-checkbox v-model="formData.loanPeriod" :localdata="formOptions.loanPeriod_localdata"></uni-data-checkbox>
            </uni-forms-item>
            <uni-forms-item name="rate" label="年利率">
              <uni-easyinput placeholder="年利率" v-model="formData.rate"></uni-easyinput>
            </uni-forms-item>
            <uni-forms-item name="way" label="还款方式">
              <uni-data-checkbox v-model="formData.way" :localdata="formOptions.way_localdata"></uni-data-checkbox>
            </uni-forms-item>
			 <view class="uni-title">合同信息</view>
            <uni-forms-item name="agreementNumber" label="合同编号">
              <uni-easyinput placeholder="合同编号" v-model="formData.agreementNumber"></uni-easyinput>
            </uni-forms-item>
      <uni-forms-item name="agreementDocs" label="合同资料上传">
       <uni-file-picker return-type="array" v-model="formData.agreementDocs"></uni-file-picker>
      </uni-forms-item>
	   <view class="uni-title">费用信息</view>
	    <uni-forms-item name="infoCharge" label="信息服务费">
	          <uni-easyinput placeholder="信息服务费" v-model="formData.infoCharge"></uni-easyinput>
	        </uni-forms-item>
	        <uni-forms-item name="fileCharge" label="资料费">
	          <uni-easyinput placeholder="资料费" v-model="formData.fileCharge"></uni-easyinput>
	        </uni-forms-item>
	        <uni-forms-item name="bridgeCharge" label="过桥费">
	          <uni-easyinput placeholder="过桥费" v-model="formData.bridgeCharge"></uni-easyinput>
	        </uni-forms-item>
	        <uni-forms-item name="otherCharge" label="其它费用">
	          <uni-easyinput placeholder="其它费用" v-model="formData.otherCharge"></uni-easyinput>
	        </uni-forms-item>
      <uni-forms-item name="remark" label="备注">
        <uni-easyinput placeholder="备注" v-model="formData.remark"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="belongTo" label="归属人">
        <uni-data-select
        		 collection="uni-id-users"
        		 field="_id as value, nickname as text"
        		 v-model="formData.belongTo"
         />
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mydb-customer-deal.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mydb-customer-deal';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "customerId": "",
        "productId": "",
        "productIdType": "",
		"limit": "",
		"transfer": "",
		"loanPeriod": "",
		"rate": "",
		"way": "",
		"agreementNumber": "",
		"agreementDocs": [],
		"infoCharge": "",
		"fileCharge": "",
		"bridgeCharge": "",
		"otherCharge": "",
        "remark": "",
        "belongTo": "",
        "lastUpdateBy": "",
        "belongDept": "",
        "updateTime": null
      }
      return {
        formData,
        formOptions: {
          "productIdType_localdata": [
                      {
                        "value": "抵押",
                        "text": "抵押"
                      },
                      {
                        "value": "信用",
                        "text": "信用"
                      },
                      {
                        "value": "财税",
                        "text": "财税"
                      },
                      {
                        "value": "法务",
                        "text": "法务"
                      },
                      {
                        "value": "现金业务",
                        "text": "现金业务"
                      },
                      {
                        "value": "其它",
                        "text": "其它"
                      }
                    ],
                    "loanPeriod_localdata": [
                      {
                        "value": "12",
                        "text": "12"
                      },
                      {
                        "value": "24",
                        "text": "24"
                      },
                      {
                        "value": "36",
                        "text": "36"
                      },
                      {
                        "value": "48",
                        "text": "48"
                      },
                      {
                        "value": "60",
                        "text": "60"
                      },
                      {
                        "value": "120",
                        "text": "120"
                      },
                      {
                        "value": "240",
                        "text": "240"
                      },
                      {
                        "value": "360",
                        "text": "360"
                      }
                    ],
                    "way_localdata": [
                      {
                        "value": "先息后本",
                        "text": "先息后本"
                      },
                      {
                        "value": "等额本息",
                        "text": "等额本息"
                      },
                      {
                        "value": "等额本金",
                        "text": "等额本金"
                      },
                      {
                        "value": "其它",
                        "text": "其它"
                      }
                    ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
	onLoad(e) {
	  if (e.customer_id) {
	    this.formData.customerId = e.customer_id
	  }
	},
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
<style scoped>
.uni-title{
	font-size: 20px;
	font-weight: bold;
	color: #333;
	margin-bottom: 20px;
}
</style>